import { type CodeType } from './../types/user.d'
import { OrderType } from '@/enum'
import {
  cancelOrder,
  deleteOrder,
  followOrUnfollow,
  getPrescriptionPic
} from '@/services/consult'
import { getMedicalOrderDetail } from '@/services/order'
import { sendMobileCode } from '@/services/user'
import type { ConsultOrderItem, FollowType } from '@/types/consult'
import type { OrderDetail } from '@/types/order'
import {
  showFailToast,
  showImagePreview,
  showSuccessToast,
  showToast,
  type FormInstance
} from 'vant'
import { onMounted, onUnmounted, ref, type Ref } from 'vue'

/**
 * 关注和取消关注
 * @param type
 */
export const useFollow = (type: FollowType = 'doc') => {
  const loading = ref(false)
  // 类型兼容: 多的可以传递给少的
  const follow = async (item: { id: string; likeFlag: 0 | 1 }) => {
    loading.value = true
    try {
      await followOrUnfollow(item.id, type)
      item.likeFlag = item.likeFlag === 1 ? 0 : 1
    } finally {
      loading.value = false
    }
  }
  return { loading, follow }
}

/**
 * 查看处方
 */
export const useShowPrescription = () => {
  const showPrescription = async (id?: string) => {
    if (!id) return showToast('当前处方没有处方单~')
    const res = await getPrescriptionPic(id)
    showImagePreview([res.data.url])
  }
  return { showPrescription }
}

/**
 * 取消订单
 */
export const useCancelOrder = () => {
  const loading = ref(false)
  const cancelConsultOrder = async (item: ConsultOrderItem) => {
    try {
      loading.value = true
      await cancelOrder(item.id)
      item.status = OrderType.ConsultCancel
      item.statusValue = '已取消'
      showSuccessToast('取消成功')
    } catch (error) {
      showFailToast('取消失败')
    } finally {
      loading.value = false
    }
  }
  return { loading, cancelConsultOrder }
}

export const useDeleteOrder = (cb: () => void) => {
  const deleteLoading = ref(false)
  const deleteConsultOrder = async (item: ConsultOrderItem) => {
    try {
      deleteLoading.value = true
      await deleteOrder(item.id)
      showSuccessToast('删除成功')
      // 删除成功后要做的事情 列表页(通知父组件删除数据) 详情页(回到列表页)
      cb && cb()
    } catch (error) {
      showFailToast('删除失败')
    } finally {
      deleteLoading.value = false
    }
  }
  return { deleteLoading, deleteConsultOrder }
}

/**
 * 获取订单详情
 * @param id 订单的id
 */
export const useOrderDetail = (id: string) => {
  const loading = ref(false)
  const order = ref<OrderDetail>()
  onMounted(async () => {
    try {
      loading.value = true
      const res = await getMedicalOrderDetail(id)
      order.value = res.data
    } finally {
      loading.value = false
    }
  })
  return { order, loading }
}

export const useSendMobileCode = (
  mobile: Ref<string>,
  type: CodeType = 'login'
) => {
  // 3. 发送短信验证码
  const time = ref(0)
  const formRef = ref<FormInstance>()
  let timerId: number
  const send = async () => {
    // 判断: 如果正在倒计时, 则不做任何操作
    if (time.value > 0) return
    // 校验输入框中的手机号码
    await formRef.value?.validate('mobile')
    // 调用发送短信验证码接口
    const res = await sendMobileCode(mobile.value, type)
    showToast({
      message: `短信验证码: ${res.data.code}`,
      duration: 3000
    })

    // -------------------开启倒计时
    time.value = 10
    window.clearInterval(timerId)
    timerId = window.setInterval(() => {
      time.value--
      if (time.value <= 0) window.clearInterval(timerId)
    }, 1000)
  }

  onUnmounted(() => {
    window.clearInterval(timerId)
  })

  return { formRef, time, send }
}
